home *** CD-ROM | disk | FTP | other *** search
/ System Booster / System Booster.iso / Mousetools / Helper / Helper.c < prev    next >
C/C++ Source or Header  |  1996-09-26  |  11KB  |  519 lines

  1. #define INTUITIONPRIVATE 1
  2. #include <devices/inputevent.h>
  3. #include <devices/input.h>
  4. #include <graphics/gfxbase.h>
  5. #include <devices/console.h>
  6. #include <libraries/reqbase.h>
  7. #include <workbench/startup.h>
  8. #include <functions.h>
  9. #include <exec/exec.h>
  10. #include <intuition/intuitionbase.h>
  11. #include <stdio.h>
  12. #include <ctype.h>
  13. #include <time.h>
  14.  
  15. #define HELP        1L<<mysignal
  16. #define TASTE        ((notepad != NULL) ? (1L<<inPort->mp_SigBit) : 0L)
  17. #define WINDOW        ((notepad != NULL) ? (1L<<notepad->UserPort->mp_SigBit) : 0L)
  18. #define ENDE        SIGBREAKF_CTRL_C
  19.  
  20. #define SCREENYSIZE    256L
  21. #define TEXTYSIZE    30L
  22.  
  23. void HandlerInterface();
  24.  
  25. struct MsgPort            *inPort = NULL, *outPort = NULL;
  26. struct IOStdReq            *inReq = NULL, *npior = NULL, *npiow = NULL;
  27. struct IntuitionBase    *IntuitionBase;
  28. struct ReqBase            *ReqBase;
  29. struct GfxBase            *GfxBase;
  30. struct Window            *notepad = NULL;
  31. struct FileRequester    MyFileReqStruct;
  32. struct Process            *mytask = NULL;
  33.  
  34. APTR                    olderrorwindow = NULL;
  35. long                    mysignal = 0L;
  36. UBYTE                    cb, text[TEXTYSIZE][80];        /* Notizblatt */
  37. int                        x, y;                            /* Cursorposition */
  38. char                    filename[FCHARS+1],
  39.                         directoryname[DSIZE+1],
  40.                         answerarray[DSIZE+FCHARS+1];
  41.  
  42. struct Interrupt        HandlerData = { { NULL,NULL,0L,51L,NULL }, NULL, &HandlerInterface };
  43.  
  44.  
  45. /**************************************************************************/
  46.  
  47.  
  48. chrout(c)
  49.   UBYTE c;
  50. {
  51.     npiow->io_Data = &c;
  52.     npiow->io_Length = 1L;
  53.     npiow->io_Command = CMD_WRITE;
  54.     DoIO(npiow); GetMsg(outPort);
  55. }
  56.  
  57. UBYTE chrin()
  58. {    UBYTE c;
  59.  
  60.     npior->io_Data = &c;
  61.     npior->io_Length = 1L;
  62.     npior->io_Command = CMD_READ;
  63.     DoIO(npior); GetMsg(inPort);
  64.     return c;
  65. }
  66.  
  67. myputs(s)
  68.   char *s;
  69. {
  70.     npiow->io_Length = strlen(s);
  71.     npiow->io_Data = s;
  72.     npiow->io_Command = CMD_WRITE;
  73.     DoIO(npiow); GetMsg(outPort);
  74. }
  75.  
  76. clrTextspeicher()
  77. {    register int i;
  78.  
  79.     for(i=0; i<TEXTYSIZE; i++)
  80.     {    setmem(&text[i],79,32);
  81.         text[i][79]=0;
  82.     }
  83.     text[TEXTYSIZE-1][78]=0;
  84. }
  85.  
  86. OpenNotepad()
  87. {    static struct NewWindow Newnotepad = {
  88.         0,0,640,SCREENYSIZE,0,1,MOUSEBUTTONS+CLOSEWINDOW,
  89.         ACTIVATE+NOCAREREFRESH+RMBTRAP+WINDOWDEPTH+WINDOWCLOSE+WINDOWDRAG,
  90.         NULL,NULL,"Notizblatt",NULL,NULL,5,5,640,SCREENYSIZE,WBENCHSCREEN };
  91.     register int i;
  92.     struct Screen *s;
  93.  
  94.     s = IntuitionBase->ActiveScreen;
  95.     if(s->Width >= 640 && s->Height >= SCREENYSIZE)
  96.     {    Newnotepad.Type = CUSTOMSCREEN;
  97.         Newnotepad.Screen = s;
  98.     }
  99.     else
  100.     {    Newnotepad.Type = WBENCHSCREEN;
  101.         Newnotepad.Screen = NULL;
  102.         WBenchToFront();
  103.     }
  104.  
  105.     if(notepad)
  106.         WindowToFront(notepad);
  107.     else
  108.     {    if(notepad = OpenWindow(&Newnotepad))
  109.         {    npiow->io_Data = notepad;
  110.             OpenDevice("console.device", 0L, npiow, 0L);
  111.             npior->io_Data = notepad;
  112.             OpenDevice("console.device", 0L, npior, 0L);
  113.             for(i=0; i<TEXTYSIZE; i++)
  114.             {    myputs(&text[i]);
  115.                 if(i<TEXTYSIZE-1) chrout(10);
  116.             }
  117.             myputs("\233H");
  118.             x = y = 0;
  119.         }
  120.     }
  121. }
  122.  
  123. struct InputEvent *myHandler(event, data)
  124.   struct InputEvent *event;
  125.   APTR data;
  126. {    register struct InputEvent *e, *oe = NULL, *ne = NULL;
  127.  
  128.     Forbid();
  129.     for(e=event; e != NULL; e = ne)
  130.     {
  131.         ne = e->ie_NextEvent;
  132.         if(e->ie_Class == IECLASS_RAWKEY)
  133.         {
  134.             if(e->ie_Code == 0x5F)        /* HELP-Taste ? */
  135.             {
  136.                 Signal(mytask, 1L<<mysignal);
  137.  
  138.                 if(oe) oe->ie_NextEvent = ne;
  139.                 else event = ne;
  140.                 e = NULL;
  141.             }
  142.         }
  143.         if(e) oe = e;
  144.     }
  145.  
  146.     Permit();
  147.     return(event);
  148. }
  149.  
  150. void HandlerInterface()
  151. {
  152. #asm
  153.     movem.l    a4,-(sp)
  154.     jsr        _geta4#
  155.     movem.l    a0/a1,-(sp)
  156.     jsr        _myHandler
  157.     addq.l    #8,sp
  158.     move.l    (sp)+,a4
  159. #endasm
  160. }
  161.  
  162. Init()
  163. {
  164.     IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library", NULL);
  165.     GfxBase = IntuitionBase->GfxBase;
  166.  
  167.     if((ReqBase = OpenLibrary("req.library", NULL)) == NULL)
  168.     {    DisplayBeep(0L);
  169.         CloseLibrary(IntuitionBase);
  170.         exit(20);
  171.     }
  172.  
  173.     inPort = CreatePort("HelperIn", NULL);
  174.     outPort = CreatePort("HelperOut", NULL);
  175.     npior = CreateStdIO(inPort);    /* req für Console lesen */
  176.     npiow = CreateStdIO(outPort);    /* req für Console schreiben */
  177.     mysignal = AllocSignal(-1L);
  178.     SetSignal(0L, 1L<<mysignal);
  179.     mytask = FindTask(NULL);
  180.     olderrorwindow = mytask->pr_WindowPtr;
  181.  
  182.     inReq = CreateStdIO(inPort);
  183.     OpenDevice("input.device", 0L, inReq, 0L);
  184.     inReq->io_Command = IND_ADDHANDLER;
  185.     inReq->io_Data = (APTR)&HandlerData;
  186.     DoIO(inReq);
  187.  
  188.     MyFileReqStruct.PathName = answerarray;
  189.     MyFileReqStruct.Dir = directoryname;
  190.     MyFileReqStruct.File = filename;
  191.     MyFileReqStruct.dirnamescolor = 2;
  192.     MyFileReqStruct.devicenamescolor = 2;
  193.     clrTextspeicher();
  194. }
  195.  
  196. Quit()
  197. {
  198.     FreeSignal(mysignal);
  199.     inReq->io_Command = IND_REMHANDLER;
  200.     inReq->io_Data = (APTR)&HandlerData;
  201.     DoIO(inReq);
  202.     CloseDevice(inReq);            /* input.device */
  203.  
  204.     if(notepad)
  205.     {    AbortIO(npior);
  206.         CloseDevice(npior);
  207.         CloseDevice(npiow);
  208.         CloseWindow(notepad);    /* notepad & console */
  209.     }
  210.  
  211.     DeleteStdIO(inReq);
  212.     DeleteStdIO(npior);
  213.     DeleteStdIO(npiow);
  214.     DeletePort(inPort);
  215.     DeletePort(outPort);        /* io-strukturen */
  216.  
  217.     CloseLibrary(ReqBase);
  218.     CloseLibrary(IntuitionBase);
  219.     mytask->pr_WindowPtr = NULL;
  220.     exit(0);
  221. }
  222.  
  223. getzchn()        /* setzt nur den req auf... */
  224. {
  225.     npior->io_Data = &cb;            /* charbuffer */
  226.     npior->io_Length = 1L;
  227.     npior->io_Command = CMD_READ;
  228.     SendIO(npior);
  229. }
  230.  
  231. int UserRequest(text,a,b,c)
  232.   char *text,*a,*b,*c;
  233. {    static struct TRStructure meintrs =
  234.     { 0,0,0,0,0,0,"Dein Helper fragt dich:",0xFFFF,0,0,0,0,0,0 };
  235.  
  236.     meintrs.Text = text;
  237.     meintrs.NegativeText = a;        /* 0 (rechts) */
  238.     meintrs.PositiveText = b;        /* 1 (links) */
  239.     meintrs.MiddleText = c;            /* 2 (mitte) */
  240.  
  241.     return TextRequest(&meintrs);
  242. }
  243.  
  244. char *extend(name,ext)
  245.   char *name, *ext;
  246. {    static char buffer[40];
  247.     char *s, *rindex();
  248.  
  249.     if(s = rindex(name, '.'))
  250.         if(strncmp(s, ext) == 0) return name;
  251.  
  252.     strcpy(buffer,name);
  253.     strcat(buffer,ext);
  254.     return buffer;
  255. }
  256.  
  257. HandleTaste()
  258. {    char com[16];
  259.     register char *s;
  260.     FILE *fp;
  261.     register int i;
  262.     long uz;
  263.     struct tm *tm;
  264.  
  265.     switch(cb)
  266.     {    case 8:
  267.             if(x!=0 || y!=0)
  268.             {    chrout(8);
  269.                 if(--x < 0)
  270.                 {    x=78;
  271.                     if(--y < 0) y=0;
  272.                 }
  273.             }
  274.         case 127:
  275.             myputs("\233P");
  276.             strcpy(&text[y][x], &text[y][x+1]);
  277.             break;
  278.         case 13:
  279.             if(++y < TEXTYSIZE)
  280.                 chrout(10);
  281.             else
  282.             {    y = TEXTYSIZE-1;
  283.                 chrout(13);
  284.             }
  285.             x = 0;
  286.             break;
  287.         case 12:
  288.             chrout(12);
  289.             clrTextspeicher();
  290.             break;
  291.         case 155:
  292.             cb = chrin();
  293.             switch(cb)
  294.             {    case 'A':
  295.                     if(--y < 0)
  296.                         y = 0;
  297.                     else
  298.                         myputs("\233A");
  299.                     break;
  300.                 case 'B':
  301.                     if(++y > TEXTYSIZE-1)
  302.                         y = TEXTYSIZE-1;
  303.                     else
  304.                         myputs("\233B");
  305.                     break;
  306.                 case 'C':
  307.                     if(++x > 78)
  308.                     {    x = 0;
  309.                         if(++y > TEXTYSIZE-1)
  310.                         {    y=TEXTYSIZE-1;
  311.                             myputs("\233A");
  312.                         }
  313.                     }
  314.                     myputs("\233C");
  315.                     break;
  316.                 case 'D':
  317.                     if(--x < 0)
  318.                     {    x = 78;
  319.                         if(--y < 0)
  320.                         {    y=0;
  321.                             myputs("\233B");
  322.                         }
  323.                     }
  324.                     myputs("\233D");
  325.                     break;
  326.                 case 'T':
  327.                     y = 0;
  328.                     sprintf(com,"\233%d;%dH",y+1,x+1);
  329.                     myputs(com);
  330.                     break;
  331.                 case 'S':
  332.                     y = TEXTYSIZE-1;
  333.                     sprintf(com,"\233%d;%dH",y+1,x+1);
  334.                     myputs(com);
  335.                     break;
  336.                 case ' ':
  337.                     cb = chrin();
  338.                     if(cb == 'A')
  339.                     {    x = 0;
  340.                         sprintf(com,"\233%d;%dH",y+1,x+1);
  341.                         myputs(com);
  342.                     }
  343.                     else
  344.                     {    x = 78;
  345.                         sprintf(com,"\233%d;%dH",y+1,x+1);
  346.                         myputs(com);
  347.                     }
  348.             }
  349.             break;
  350.         case 11:            /* K = Kill Zeile */
  351.             myputs("\233M\233L");
  352.             setmem(&text[y][0],79,32);
  353.             text[y][79] = 0;
  354.             break;
  355.         case 5:                /* E = 1 Zchn Einfügen */
  356.             myputs("\233@");
  357.             movmem(&text[y][x],&text[y][x+1],78-x);
  358.             text[y][x] = 32;
  359.             break;
  360.         case 19:            /* S = Speichern */
  361.             MyFileReqStruct.Flags = FRQCACHINGM+FRQINFOGADGETM+FRQSAVINGM;
  362.             MyFileReqStruct.Title = "Notizblatt abspeichern:";
  363.             strcpy(directoryname, "T:");
  364.             strcpy(MyFileReqStruct.Show, "*.note");
  365.             if(FileRequester(&MyFileReqStruct))
  366.             {    if(fp = fopen(s = extend(answerarray, ".note"), "w+"))
  367.                 {    fwrite(text, (int)(80*TEXTYSIZE), 1, fp);
  368.                     fclose(fp);
  369.                 }
  370.                 else SimpleRequest("Ich kann %s nicht öffnen.", s);
  371.             }
  372.             break;
  373.         case 26:            /* Z = Zurückholen */
  374.             MyFileReqStruct.Flags = FRQCACHINGM+FRQINFOGADGETM+FRQLOADINGM;
  375.             MyFileReqStruct.Title = "Notizblatt zurückholen:";
  376.             strcpy(directoryname, "T:");
  377.             strcpy(MyFileReqStruct.Show, "*.note");
  378.             if(FileRequester(&MyFileReqStruct))
  379.             {    if(fp = fopen(s = extend(answerarray, ".note"), "r+"))
  380.                 {    fread(text, (int)(80*TEXTYSIZE), 1, fp);
  381.                     fclose(fp);
  382.                     myputs("\f");
  383.                     for(i=0; i<TEXTYSIZE; i++)
  384.                     {    myputs(&text[i]);
  385.                         if(i<TEXTYSIZE-1) chrout(10);
  386.                     }
  387.                     myputs("\233H");
  388.                     x = y = 0;
  389.                 }
  390.                 else SimpleRequest("Ich kann %s nicht öffnen.", s);
  391.             }
  392.             break;
  393.         case 21:                /* U = Uhrzeit */
  394.             if(x > 73) break;
  395.             uz = time(0L);
  396.             tm = gmtime(&uz);
  397.             sprintf(com, "%02d:%02d", tm->tm_hour, tm->tm_min);
  398.             myputs(com);
  399.             movmem(com, &text[y][x], 5);
  400.             x += 5;
  401.             break;
  402.         case 4:                    /* D = Datum */
  403.             if(x > 70) break;
  404.             uz = time(0L);
  405.             tm = gmtime(&uz);
  406.             sprintf(com,"%02d.%02d.%02d", tm->tm_mday, tm->tm_mon+1, tm->tm_year);
  407.             myputs(com);
  408.             movmem(com, &text[y][x], 8);
  409.             x += 8;
  410.             break;
  411.         default:
  412.             if( ((cb>31)&&(cb<127)) || ((cb>160)&&(cb<255)) )
  413.             {    if( (x==78) && (y==TEXTYSIZE-1) )
  414.                     chrout(7);
  415.                 else
  416.                 {
  417.                     chrout(cb);
  418.                     text[y][x] = cb;
  419.                     if(++x > 78)
  420.                     {    x=0;
  421.                         ++y;
  422.                     }
  423.                 }
  424.             }
  425.     }
  426. }
  427.  
  428. main(argc, argv)
  429.   int argc;
  430.   char *argv[];
  431. {    register long signals;
  432.     register int ix, iy, i;
  433.     char more[32], com[16];
  434.     BOOL wbtf = TRUE;
  435.     struct IntuiMessage *m;
  436.     ULONG class;
  437.  
  438.     Init();
  439.  
  440.     strcpy(more, "SYS:Utilities/More");
  441.     for(i=1; i<argc; i++)
  442.     {    if(argv[i][0] == '?')
  443.         {    SimpleRequest("»Helper« Version 1.01d von Michael Balzer 1990\n"
  444.                           "HotKey ist HELP (was sonst?), und vom CLI aus\n"
  445.                           "gibt's folgende Optionen:\n"
  446.                           "-mPRG{Anzeigeprg} -f0/1{WorkBenchToFront}\n"
  447.                           "Defaultwerte: -mSYS:Utilities/More -f1");
  448.             Quit();
  449.         }
  450.         else if(strncmp(argv[i], "-m", 2) == 0)
  451.             strcpy(more, &argv[i][2]);
  452.         else if(strncmp(argv[i], "-f", 2) == 0)
  453.             wbtf = (argv[i][2] == '0') ? FALSE : TRUE;
  454.     }
  455.  
  456.     do
  457.     {    signals = Wait(HELP | TASTE | WINDOW | ENDE);
  458.         mytask->pr_WindowPtr = IntuitionBase->ActiveScreen->FirstWindow;
  459.  
  460.         if(signals & HELP)
  461.         {    switch(UserRequest("Was möchtest du?", " Text ansehen ", " Notizblatt ", " Farben ändern "))
  462.             {    case 0:
  463.                     MyFileReqStruct.Flags = FRQCACHINGM+FRQINFOGADGETM+FRQLOADINGM;
  464.                     MyFileReqStruct.Title = "Bitte Text wählen:";
  465.                     strcpy(directoryname, "HELP:");
  466.                     MyFileReqStruct.Show[0] = 0;
  467.                     if(FileRequester(&MyFileReqStruct))
  468.                     {    if(wbtf) WBenchToFront();
  469.                         if(fexecl(more, more, answerarray, NULL) == -1)
  470.                             SimpleRequest("%s nicht ausführbar!", more);
  471.                         if(((struct Window *)mytask->pr_WindowPtr)->WScreen != IntuitionBase->FirstScreen)
  472.                             WBenchToBack();
  473.                     }
  474.                     break;
  475.                 case 1:
  476.                     OpenNotepad();
  477.                     getzchn();            /* Request für Tastendruck aufsetzen */
  478.                     break;
  479.                 case 2:
  480.                     ColorRequester(1L);
  481.             }
  482.         }
  483.  
  484.         if(signals & TASTE)
  485.         {    GetMsg(inPort);        /* Reply entfernen */
  486.             HandleTaste();
  487.             cb=0;
  488.             getzchn();
  489.         }
  490.  
  491.         if(signals & WINDOW)
  492.         {    if(m = (struct IntuiMessage *)GetMsg(notepad->UserPort))
  493.             {    class = m->Class;
  494.                 ix = m->MouseX; iy = m->MouseY;
  495.                 ReplyMsg(m);
  496.                 if(class == CLOSEWINDOW)
  497.                 {    AbortIO(npior);
  498.                     CloseDevice(npior);
  499.                     CloseDevice(npiow);
  500.                     CloseWindow(notepad);
  501.                     notepad = NULL;
  502.                 }
  503.                 else if(class == MOUSEBUTTONS)
  504.                 {    x = (ix-4)/8;
  505.                     if(x>78) x=78;
  506.                     if(x<0) x=0;
  507.                     y = (iy-11)/8;
  508.                     if(y>TEXTYSIZE-1) y=TEXTYSIZE-1;
  509.                     if(y<0) y=0;
  510.                     sprintf(com,"\233%d;%dH",y+1,x+1);
  511.                     myputs(com);
  512.                 }
  513.             }
  514.         }
  515.     } while(!(signals & ENDE));
  516.  
  517.     Quit();
  518. }
  519.